/*
 *
 * JavaScript - List
 */

$().ready( function() {

	// 开关
    $("#contentDiv").on("change",".switchbox",function() {
		var $this = $(this);
		var $status= $this.prop("checked");
		var $url = $this.data("url");
		var $id = $this.data("id");
		$.ajax({
			url: $url,
			type: "POST",
			data: {"id":$id,"value":$status},
			dataType: "json",
			cache: false,
			success: function(response) {
				if (response.type == "success") {
					swal({title:'', text:'操作成功',icon: 'success'});
				}
			}
		});
	});

	//排序
    $("#contentDiv").on("click","#sortButton",function() {
        var $this = $(this);
        var $url = $this.attr("url");
        var $sortArry = [];
        $.each($("#listTr tr"),function(index,item){
            var $sorts = $(item).find("input[name='sorts']");
            var $sort = $sorts.val();
            var $oldsort = $sorts.data("oldsort");
            var $id = $sorts.data("id");
            if($sort!=$oldsort){
                var $sortReg = /^[1-9]\d*|0$/;
                if (!($sortReg.test($sort))) {
                   $sort = "0";
                }
                var $json = {"id":$id,"sort":$sort};
                $sortArry.push($json);
            }
        });
        if($sortArry.length<1){
            swal({title:'', text:'数据未修改!',icon: 'warning'});
            return false;
        }
        $.ajax({
            url: $url,
            type: "POST",
            data: {sortArray: JSON.stringify($sortArry)},
            dataType: "json",
            cache: false,
            success: function(response) {
                if (response.type == "success") {
                	swal({title:'', text:'操作成功',icon: 'success'});
                }else{
                    swal({title:'', text:message.msg,icon: 'error'});
                }
            }
        });
        return false;
    });

	// 删除
    $("#contentDiv").on("click","#deleteButton",function() {
		var $this = $(this);
		var $url = $this.attr("url");
		if ($this.hasClass("disabled")) {
			return false;
		}
		var $checkedIds = $("#listTable input[name='ids']:enabled:checked");
		if(confirm("您确定要删除吗？")){
			$.ajax({
				url: $url,
				type: "POST",
				data: $checkedIds.serialize(),
				dataType: "json",
				cache: false,
				success: function(response) {
					if (response.type == "success") {
						$checkedIds.closest("tr").remove();
						swal({title:'', text:'操作成功',icon: 'success'});
					}
					$("#deleteButton").addClass("disabled");
					$("#selectAll").prop("checked", false);
					$checkedIds.prop("checked", false);
				}
			});
		}
		return false;
	});

	// 刷新
    $("#contentDiv").on("click","#refreshButton",function() {
		location.reload(true);
		return false;
	});

	// 全选
    $("#contentDiv").on("click","#selectAll",function() {
		var $this = $(this);
		var $enabledIds = $("#listTable input[name='ids']:enabled");
		if ($this.prop("checked")) {
			$enabledIds.prop("checked", true);
			if ($enabledIds.filter(":checked").size() > 0) {
				$("#deleteButton").removeClass("disabled");
				$("#listTable tr:gt(0)").addClass("selected");
			} else {
				$("#deleteButton").addClass("disabled");
			}
		} else {
			$enabledIds.prop("checked", false);
			$("#deleteButton").addClass("disabled");
			$("#listTable tr:gt(0)").removeClass("selected");
		}
	});

	// 选择
    $("#contentDiv").on("click","#listTable input[name='ids']",function() {
		var $this = $(this);
		if ($this.prop("checked")) {
			$this.closest("tr").addClass("selected");
			$("#deleteButton").removeClass("disabled");
		} else {
			$this.closest("tr").removeClass("selected");
			if ($("#listTable input[name='ids']:enabled:checked").size() > 0) {
				$("#deleteButton").removeClass("disabled");
			} else {
				$("#deleteButton").addClass("disabled");
			}
		}
	});

	// 页码
    $("#contentDiv").on("keypress","#pageNumber",function(event) {
		return (event.which >= 48 && event.which <= 57) || event.which == 8 || (event.which == 13 && $(this).val().length > 0);
	});

	// 表单提交
    $("#contentDiv").on("submit","#listForm",function() {
		if (!/^\d*[1-9]\d*$/.test($("#pageNumber").val())) {
			$("#pageNumber").val("1");
		}
		if(parseInt($("#pageNumber").val())>parseInt($("#totalPage").val())){
			$("#pageNumber").val($("#totalPage").val());
		}
	});

	// 页码跳转
	$.pageSkip = function(pageNumber) {
		$("#pageNumber").val(pageNumber);
		$("#listForm").submit();
		return false;
	};
});